// pages/mine/mine.js

var WxParse = require("../../wxParse/wxParse.js");
var api = require("../../api.js"),
    e = require("../../utils/util.js"),
    r = "",
    app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
      windowWidth: 0, //页面视图宽度
      windowHeight: 0, //视图高度
      userList:[],
      pageIndex:1,
      loading: false, //第一次加载，设置true  
      loadingComplete:false, //把“没有数据”设为false，隐藏  
      isFromSearch: true, 
    },
    onLoad: function (options) {
         r =this;
         wx.getSystemInfo({
          success: function (res) {
          console.log(res)
          var windowHeight = res.windowHeight;
          //两列，每列的图片宽度
          var height = (windowHeight - 260) ;
          r.setData({
           windowHeight: height,
          }, function () {
           r.initData(); //初始化数据
          });
          },
         })
      },
      initData:function(){
        wx.showLoading({
          title: '加载中...',
         });
         var t= new Object();
         t.openId = getApp().globalData.wxUserInfo.openId;
         t.pageIndex = 1,t.pageSize=10;
         var dataList= [];
         wx.request({
          url:api.mine.subordinateCopartner,
          data:t,
          success:function(res){
            if(res.statusCode==200){
             if(res.data.code==200){
              var n = res.data.data;
              if(n.length!=0){
                console.log(11111111)
                var checkStatus='';
                n.length<10?checkStatus=false:true; 
                setTimeout(()=>{
                 r.setData({
                  userList: res.data.data,
                   loading:checkStatus,
                   loadingComplete:!checkStatus,
                   hidden:false
                 },function(){
                  wx.hideLoading();
                });
                },500)
              }else{
                setTimeout(function(){
                  r.setData({
                    userList:[],
                    loadingComplete:false,
                    loading:false,
                    hidden:true
                   },function(){
                     wx.hideLoading();
                   }
                   )
                },1000)
              }
             
             
             }
                 
            }
          }
        })
         
      }, 
      // 加载数据
  loading(){
    var t = new Object();
    var dataList= [];
    t.openId = getApp().globalData.wxUserInfo.openId;

    t.pageIndex = r.data.pageIndex,t.pageSize=10;
    wx.request({
      url: api.mine.subordinateCopartner,
      data:t,
      success: function(res) {
        if(res.statusCode==200){
          if(res.data.code==200){
            var n = res.data.data;
            if(n.length>0){
              r.data.isFromSearch ? dataList = n : dataList=r.data.userList.concat(n)  
             setTimeout(()=>{
              r.setData({
                userList: dataList,
                loading:true,
              });
             },500)
            }else{
              setTimeout(()=>{
                r.setData({
                  loadingComplete:true,
                  loading:false,
                })
              },500)
             
            }
        
         
          }
        }
      }
    });
  },
        /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let _that = this
    if(_that.data.loading && !_that.data.loadingComplete){
        _that.setData({
          pageIndex:_that.data.pageIndex+1,
          isFromSearch:false
         });
         _that.loading();
    };
  },   
});